<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/7/27
 * Time: 13:35
 */

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\Log;

class AllActivitySku extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'all_activity_sku';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    const STATUS_DESC = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_DELETE => '已删除'
    ];

    const NOT_USE_COMMISSION = 0;
    const USE_COMMISSION = 1;

    const COMMISSION_DESC = [
        self::NOT_USE_COMMISSION => '不使用佣金',
        self::USE_COMMISSION => '使用佣金'

    ];

    const NOT_USE_ACTIVITY_STOCK = 0;
    const USE_ACTIVITY_STOCK = 1;

    const ACTIVITY_STOCK_DESC = [
        self::NOT_USE_COMMISSION => '不使用活动库存',
        self::USE_COMMISSION => '使用活动库存'

    ];

    public static function updateActivitySku($activityId, $typeId, $skuInfo)
    {
        $checkGoods = self::get('id', [
            'AND' => [
                'activity_id' => $activityId,
                'type' => $typeId,
                'goods_id' => $skuInfo['goods_id'],
                'sku_id' => $skuInfo['sku_id'],
                'status' => self::STATUS_NORMAL
            ]

        ]);

        $skuInfo['edittime'] = date('Y-m-d H:i:s');
        if ($checkGoods) {
            $result = self::update($skuInfo, [
                'AND' => [
                    'activity_id' => $activityId,
                    'type' => $typeId,
                    'sku_id' => $skuInfo['sku_id'],
                    'status' => self::STATUS_NORMAL
                ]
            ])->rowcount();
        } else {
            $skuInfo['addtime'] = date('Y-m-d H:i:s');
            $result = self::insert($skuInfo)->rowcount();
        }


        if ($result) {
            return $skuInfo['sku_id'];
        }

        return false;
    }

    public static function updateActivity($activityId, $typeId, $activityAttr)
    {
        $check = self::get('id', [
            'AND' => [
                'activity_id' => $activityId,
                'type' => $typeId,
                'status' => self::STATUS_NORMAL
            ]

        ]);

        $activityAttr['edittime'] = date('Y-m-d H:i:s');

        if ($check) {
            $result = self::update($activityAttr, [
                'AND' => [
                    'activity_id' => $activityId,
                    'type' => $typeId,
                    'status' => self::STATUS_NORMAL
                ]
            ])->rowcount();
        } else {
            $activityAttr['addtime'] = date('Y-m-d H:i:s');
            $result = self::insert($activityAttr)->rowcount();
        }


        if (!$result) {
            return false;
        }

        return true;
    }

    public static function delActivitySku($activityId, $typeId)
    {
        $result = self::update(['status' => self::STATUS_DELETE, 'real_end_time' => date('Y-m-d H:i:s')], [
            'AND' => [
                'activity_id' => $activityId,
                'type' => $typeId,
                'status' => self::STATUS_NORMAL
            ]
        ])->rowcount();

        if ($result) {
            return true;
        }
        return false;
    }

    public static function delNotInGoodsIds($activityId, $typeId, array $skuIds)
    {
        $result = self::update(['status' => self::STATUS_DELETE], [
            'AND' => [
                'activity_id' => $activityId,
                'type' => $typeId,
                'sku_id[!]' => $skuIds
            ]
        ])->rowcount();

        if ($result) {
            return true;
        }
        return false;
    }

    public static function getActivitySkuInfoByGoodsIds($goodsIds, $field = "*")
    {
        $time = date('Y-m-d H:i:s');
        $skuInfo = self::select($field, [
            'AND' => [
                'goods_id' => $goodsIds,
                'end_time[>]' => $time,
                'status' => self::STATUS_NORMAL,
            ]
        ]);

        $list = [];

        foreach ($goodsIds as $goodsId) {
            $list[$goodsId] = [];
        }

        foreach ($skuInfo as $info) {
            $list[$info['goods_id']][] = $info;
        }

        return $list;
    }

    public static function getActivityInfoBySkuIds($skuIds, $field = "*")
    {
        $time = date('Y-m-d H:i:s');
        return self::select($field, [
            'AND' => [
                'sku_id' => $skuIds,
                'start_time[<=]' => $time,
                'end_time[>=]' => $time,
                'status' => self::STATUS_NORMAL
            ]
        ]);
    }

    public static function delActivitySkuById($activityId, $typeId, $goodsId)
    {
        $isCanDel = self::get('id', [
            'activity_id' => $activityId,
            'type' => $typeId,
            'goods_id' => $goodsId,
            'status' => self::STATUS_NORMAL,
        ]);
        if (!$isCanDel) {
            return true;
        }

        $result = self::update(['status' => self::STATUS_DELETE, 'real_end_time' => date('Y-m-d H:i:s')], [
            'activity_id' => $activityId,
            'type' => $typeId,
            'goods_id' => $goodsId,
            'status' => self::STATUS_NORMAL,
        ])->rowcount();

        if ($result) {
            return true;
        }
        return false;
    }

    public static function delActivitySkuBySkuId($activityId, $typeId, $skuId)
    {
        $result = self::update(['status' => self::STATUS_DELETE, 'real_end_time' => date('Y-m-d H:i:s')], [
            'activity_id' => $activityId,
            'type' => $typeId,
            'sku_id' => $skuId
        ])->rowcount();

        if ($result) {
            return true;
        }
    }

    public static function updateActualStock($stockInfo)
    {
        foreach ($stockInfo as $item) {
            self::update(['actual_stock' => $item['stock']], [
                'activity_id' => $item['promo_id'],
                'type' => $item['promo_type'],
                'goods_id' => $item['goods_id'],
                'sku_id' => $item['sku_id'],
            ])->rowcount();
        }
    }

    public static function getActivityInfoByGoodsId($activityId, $typeId, $goodsId, $field = '*')
    {
        return self::select($field, [
            'AND' => [
                'activity_id' => $activityId,
                'type' => $typeId,
                'goods_id' => $goodsId,
                'status' => self::STATUS_NORMAL
            ]
        ]);
    }

    public static function getActtivityInfoByGoodsIdsBak($goodsIds, $type = [], $field = '*')
    {
        $time = date('Y-m-d H:i:s');
        $defaultWhere = [
            'goods_id' => $goodsIds,
            'status' => self::STATUS_NORMAL,
            'end_time[>=]' => $time
        ];
        $where = [
            'type' => $type
        ];
        if (array_filter($where)) {
            $defaultWhere = array_merge($defaultWhere, $where);
        }

        $skuInfo = self::select($field, [
            'AND' => $defaultWhere
        ]);

        $list = [];
        foreach ($goodsIds as $goodsId) {
            $list[$goodsId] = [];
        }

        foreach ($skuInfo as $info) {
            $list[$info['goods_id']][] = $info;
        }

        return $list;
    }

    public static function getActivityInfoBySkuIdsBak($skuIds, $activityInfo, $field = "*")
    {
        $time = date('Y-m-d H:i:s');
        $defaultWhere = [
            'sku_id' => $skuIds,
            'preview_start_time[<=]' => $time,
            'end_time[>=]' => $time,
            'status' => self::STATUS_NORMAL,
        ];

        $where = [
            'activity_id' => isset($activityInfo['activity_id']) ? $activityInfo['activity_id'] : 0,
            'type' => isset($activityInfo['type']) ? $activityInfo['type'] : 0,
            'type[!]' => isset($activityInfo['not_activity_type']) ? $activityInfo['not_activity_type'] : 0,
        ];

        $list = self::select($field, [
            'AND' => array_filter(array_merge($defaultWhere, $where))

        ]);

        return $list ?: [];
    }

    public static function getCheckActivityList($goodsId, $type)
    {
        return self::select('*', [
            'AND' => [
                'goods_id' => $goodsId,
                'type' => $type,
            ]
        ]);
    }

    public static function getList($where = [], $order = [], $limit = [], $field = '*')
    {
        $param = [];
        $defaultWhere = ['status' => self::STATUS_NORMAL];
        if ($where) {
            $param['AND'] = array_merge($where, $defaultWhere);
        }
        if ($order) {
            $param['ORDER'] = $order;
        }
        if ($limit) {
            $param['LIMIT'] = $limit;
        }

        $list = self::select($field, $param);

        return $list;
    }
}